/**
 * 矩阵旋转工具函数
 * @param matrix 需要旋转的矩阵
 * @param flag 旋转方向，true为顺时针，false为逆时针
 */
export const rotateMatrix = (matrix: number[][], flag: boolean = true) => {
  const rows = matrix.length;
  const cols = matrix[0].length;
  const rotatedMatrix: number[][] = [];
  if (flag) {
    // 顺时针旋转90度
    for (let j = 0; j < cols; j++) {
      const newRow: number[] = [];
      for (let i = rows - 1; i >= 0; i--) {
        newRow.push(matrix[i][j]);
      }
      rotatedMatrix.push(newRow);
    }
  } else {
    // 逆时针旋转90度
    for (let j = cols - 1; j >= 0; j--) {
      const newRow: number[] = [];
      for (let i = 0; i < rows; i++) {
        newRow.push(matrix[i][j]);
      }
      rotatedMatrix.push(newRow);
    }
  }
  return rotatedMatrix;
};
